/*
 * Copyright (C) 2019 ~ 2022 Uniontech Technology Co., Ltd.
 *
 * Author:     zhangbingyi <zhangbingyi@uniontech.com>
 *
 * Maintainer: zhangbingyi <zhangbingyi@uniontech.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#pragma once

#include "exefilesprotectionmodel.h"
#include "exefilesprotectionitem.h"
#include "exefilesprotectiondelegate.h"
#include "exefilesprotectioninfo.h"
#include "exefilespolicykithelper.h"
#include "modelsettingchangedialog.h"
#include "addtrustaskdialog.h"

//#include <DFrame>
//#include <DTableView>
//#include <DSuggestButton>


#include <QStandardItemModel>
#include <QFrame>
#include <QCheckBox>

class QTableView;

const QStringList kFileNameList = {
    "application/x-binary",
    "application/x-elf",
    "application/x-executable",
    "application/x-pie-executable",
    "text/x-sh",
    "text/x-script.sh",
    "application/x-sh",
    "text/x-shellscript",
    "text/x-script.python",
    "application/x-bytecode.python",
    "text/x-python"};

class ExeFilesProtectionWidget : public QFrame
{
    Q_OBJECT
public:
    explicit ExeFilesProtectionWidget(ExeFilesProtectionModel *model, QWidget *parent = nullptr);
    ~ExeFilesProtectionWidget();

public Q_SLOTS:
    // 判断全选按钮状态
    void checkBoxAllStatus();
    // 接收信任当前状态
    void acceptTrustFile(const QModelIndex &index);

    // 添加执行文件
    void addExeFile();
    // 加载表格数据
    void loadTableData(QList<ExeFilesInfo> infoList);
    // 移除保护
    void deleteExeFiles();

    // 接收下拉框改变状态
    void acceptComboBoxChangeIndex(int nIndex);

private:
    // 初始化界面
    void initUI();
    // 初始化数据
    void initData();

    // 前端检查加入的文件类型
    bool verifyContentFileType(const QString &path);

    // 全选/反选
    void selectAll(bool);
    // 显示统计信息
    void showTotalInfo(int nCount, int nSelectCount);

Q_SIGNALS:
    void requestConfirmRemoveExeFiles(QStringList sFiles);
    void requestConfirmTrustExeFiles(const QString &);
    void requestConfirmModelSetting(int model);

public slots:
    void onPolicyNotifyRemoveExeFiles(bool result, QStringList sFiles);
    void onPolicyNotifyTrustExeFiles(bool result, const QString &file);
    void onPolicyNotifyModelSetting(bool result, int model);
    void sendExeFilesModelTurnOff();
    void onDialogClose();
    void sendAddTrustFile();
    void onAddTrustDialogClose();
    void addressChangeDialogHelp(int index, QString);
private:
    ExeFilesProtectionModel *m_model;
    ExeFilesProtectionItem *m_exeFilesItem;

    QTableView *m_tableView;
    QStandardItemModel *m_tableModel;
    ExeFilesProtectionDelegate *m_exeFilesProtectionDelegate;

    QCheckBox *m_globalCheckBox; // 全选框
    // 统计数据
    QLabel *m_counterLable;
    // 添加按钮
    QPushButton *m_addContentButton;
    QPushButton *m_removeContentButton;

    // 权限验证
    QThread m_polKitHelperThread;
    ExeFilesPolicyKitHelper *m_policyKitHelper;

    ModelSettingChangeDialog *m_modelSettingChangeDialog;
    bool m_isSettingModelFlag;

    AddTrustAskDialog *m_addTrustAskDialog;
    bool m_isAddTrustFlag;
};
